package cn.psh.basic.controller;


import cn.psh.basic.dto.LoginDto;
import cn.psh.basic.util.AjaxResult;
import cn.psh.basic.util.exception.BusinessException;
import cn.psh.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    @RequestMapping("/account")
    public AjaxResult loginAccount(@RequestBody LoginDto dto) {
        try {
            Map<String, Object> map = logininfoService.account(dto);
            return AjaxResult.OJBK().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统繁忙，稍后重试~~~");
        }
    }


    @GetMapping("/wechat/{code}")
    public AjaxResult wechatLogin(@PathVariable("code") String code){
        try {
            return logininfoService.wechatLogin(code);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统繁忙，稍后重试~~~");
        }

    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String> map){
        try {
            System.out.println(map);
            return logininfoService.wechatBinder(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("系统繁忙，稍后重试~~~");
        }
    }
}
